package com.huike.web.controller.system;

import com.huike.clues.domain.SysDictType;
import com.huike.clues.domain.dto.SystemDictTypeDto;
import com.huike.clues.service.ISysDictService;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.domain.entity.SysDictTypeDTO;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/dict")
public class SysDictTypeController extends BaseController {

    @Autowired
    private ISysDictService sysDictService;
    /**
     * 新增字典类型
     */
    @PostMapping("/type")
    public AjaxResult addType(@RequestBody SysDictType dictData){

        sysDictService.save( dictData);
        return  AjaxResult.success();
    }
    /**
     * 修改字典类型
     */
    @PutMapping("/type")
    public AjaxResult updateType(@RequestBody SystemDictTypeDto dto){

        return AjaxResult.success(sysDictService.updateType(dto));
    }
    /**
     * 清空缓存
     */
    @DeleteMapping("/type/clearCache")
    public AjaxResult clearCache() {
        try {
            sysDictService.clearCache();
            return AjaxResult.success();
        } catch (Exception e) {
            return AjaxResult.error("缓存清理失败"+e.getMessage());
        }
    }
    /**
     * 导出数据
     */
    @GetMapping("/type/export")
    public AjaxResult export(SysDictTypeDTO dictType){

        List< SysDictTypeDTO>list= sysDictService.selectDictDataList(dictType);
        ExcelUtil<SysDictTypeDTO> util = new ExcelUtil<SysDictTypeDTO>(SysDictTypeDTO.class);
        return util.exportExcel(list, "字典类型");


    }
    /**
     * 分页查询数据字典类型信息
     */
    @GetMapping("/type/list")
    public TableDataInfo list(SysDictTypeDTO dictType){

        List<SysDictTypeDTO> list= sysDictService.selectDictDataList(dictType);
        return getDataTable( list);
    }
    /**
     * 获取字典选择框列表
     */
    @GetMapping("/type/optionselect")
    public AjaxResult optionselect()
    {
        List<SysDictType> list = sysDictService.selectDictTypeAll();
        return AjaxResult.success(list);
    }
    /**
     * 删除字典类型
     */
    @DeleteMapping("/type/{dictIds}")
    public AjaxResult remove(@PathVariable String[] dictIds)
    {

        return AjaxResult.success(sysDictService.removeid(dictIds));
    }
    /**
     * 查询字典类型详细信息
     */
    @GetMapping(value = "/type/{dictId}")
    public AjaxResult getInfo(@PathVariable Long dictId)
    {

        return AjaxResult.success(sysDictService.getById(dictId));
    }

}
